<?php
namespace app\admin\controller;
use app\BaseController;
use app\common\model\FirmStyleModel;
use app\common\model\FirmModel;
use think\facade\Request;
use app\common\OpenApi;

class Miniapp extends BaseController{
    public function getInfo(){//获取小程序信息
        list('type' => $type,'secret_key' => $key) = FirmModel::field('type,secret_key')->where('firm_id',$this->request->firm_id)->find()->toArray();
        if($type == 1 && $key === 0){
            $data = [
                'domain' => $_SERVER['SERVER_NAME'],
                'firm_id' => $this->request->firm_id
            ];
            $OpenApi = new OpenApi($this->request->firm_id);
            $res = $OpenApi->getKey($data);
            if($res['code'] == 1){
                return error($res['msg']);
            }
            $key = $res['data']['key'];
            $FirmModel = FirmModel::find($this->request->firm_id);
            $FirmModel->secret_key = $key;
            $FirmModel->save();
        }
        if($type == 1){
            $OpenApi = new OpenApi($this->request->firm_id);
            $res = $OpenApi->openApiPost('miniapp/getInfo');
            if($res['code'] == 1){
                return error($res['msg']);
            }
        }
        
        $res['data']['type'] = $type;
        return success($res['data']);
    }
    public function commit(){//上传小程序
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/commit');
        if($res['code'] == 1){
            return error($res['msg']);
        }
        return success('上传成功');
    }
    public function getTrialQRCode(){//获取体验二维码
        $dir = root_path().'/public';
        $path = '/upload/qr/'.$this->request->firm_id.'/';
        !is_dir($dir.$path) && mkdir($dir.$path, 0755, true);
        $path = $path.'text.png';
        if(!file_exists($dir.$path)){
            $OpenApi = new OpenApi($this->request->firm_id);
            $res = $OpenApi->openApiPostImage('miniapp/getTrialQRCode');
            file_put_contents($dir.$path, $res);
        }
        return success(['qr_url'=>'https://'.$_SERVER['SERVER_NAME'].$path]);
    }
    
    public function submitAudit(){//提交审核
        $first_id = $this->request->param('first_id/d',0);
        if($first_id === 0){
            return error('分类错误');
        }
        $first_class = $this->request->param('first_class/s','');
        if($first_class === ''){
            return error('分类错误');
        }
        $second_id = $this->request->param('second_id/d',0);
        if($second_id === 0){
            return error('分类错误');
        }
        $second_class = $this->request->param('second_class/s','');
        if($second_class === ''){
            return error('分类错误');
        }
        $data = [
            'first_id' => $first_id,
            'second_id' => $second_id,
            'first_class' => $first_class,
            'second_class' => $second_class
        ];
        
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/submitAudit',$data);
        if($res['code'] == 1){
            return error($res['msg']);
        }
        return success($res['data']);
    }
    
    public function speedupaudit(){//加急审核
    
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/speedupaudit');
        if($res['code'] == 1){
            return error($res['msg']);
        }
        return success($res['data']);
    }
    
    public function undocodeaudit(){//发布上线
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/undocodeaudit');
        if($res['code'] == 1){
            return error($res['msg']);
        }
        return success($res['data']);
    }
    
    public function release(){//发布上线
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/release');
        if($res['code'] == 1){
            return error($res['msg']);
        }
        $FirmModel = FirmModel::find($this->request->firm_id);
        if($FirmModel->parent_id != $this->request->firm_id){
            $FirmModel->parent_id = $this->request->firm_id;
            $FirmModel->save();
        }
        return success($res['data']);
    }
    
    public function settingCategory(){//获取已设置的类目
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/getSettingCategories');
        if($res['code'] == 1){
            return error($res['msg']);
        }
        return success($res['data']);
    }
    
    
    //删除类目
    public function deletecategory(){
        $first = $this->request->param('first/d',0);
        if(empty($first)){
            return error('参数错误');
        }
        $second = $this->request->param('second/d',0);
        if(empty($second)){
            return error('参数错误');
        }
        $data = [
            'first' => $first,
            'second' => $second
        ];
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/deletecategory',$data);
        if($res['code']){
            return error('类目删除失败');
        }else{
            return success('类目删除成功');
        }
    }
    // 一件添加类目
    public function addcategory(){
        $first = $this->request->param('first/d',287);
        if(empty($first)){
            return error('参数错误');
        }
        $second = $this->request->param('second/d',298);
        if(empty($second)){
            return error('参数错误');
        }
        $data = [
            'first' => $first,
            'second' => $second
        ];
        $OpenApi = new OpenApi($this->request->firm_id);
        $res = $OpenApi->openApiPost('miniapp/addcategory',$data);
        if($res['code']){
            return error('类目添加失败');
        }else{
            return success('类目添加成功');
        }
    }
}